package dept

import (
	"context"
	"go-zero-project/services/user/rpc/pb"

	"go-zero-project/services/user/api/internal/svc"
	"go-zero-project/services/user/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type PageListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDeptPageListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PageListLogic {
	return &PageListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *PageListLogic) DeptPageList(req *types.DeptPageListReq) (resp *types.DeptPageListResp, err error) {
	// 1.builder rpc request
	pageListReq := pb.DeptPageListReq{PageSize: int32(req.PageSize), PageNum: int32(req.PageNum), DeptName: req.DeptName}
	// 2.request rpc
	pageList, err := l.svcCtx.UserCenterClient.DeptPageList(l.ctx, &pageListReq)
	if err != nil {
		return nil, err
	}
	// 3.return obj
	resp = new(types.DeptPageListResp)
	resp.TotalPage = int(pageList.TotalPage)
	resp.CurrentPage = int(pageList.CurrentPage)
	dataList := pageList.Data
	if len(dataList) < 1 {
		return resp, nil
	}
	var returnList []types.DeptInfoResp
	for _, v := range dataList {
		info := types.DeptInfoResp{DeptId: v.DeptId,
			ParentId: v.ParentId, ParentName: v.ParentName,
			Name: v.Name, OrderNum: int(v.OrderNum), SubNodeNum: int(v.SubNodeNum)}
		returnList = append(returnList, info)
	}
	resp.List = returnList
	return resp, nil
}
